package com.baizhi.action;

import com.alibaba.fastjson.JSON;
import com.baizhi.Student;
import com.baizhi.service.StudentService;
import com.baizhi.service.impl.StudentServiceImpl;
import com.github.pagehelper.PageInfo;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class UserAction {
    private String name;
    private Student u;
    private int pageIndex=1;
    private int pageSize=3;
    private StudentService  a;
    public void ma ()  {
        //响应浏览器
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (name.equals("张三")) {
                writer.print("true");
            }else{
                writer.print("false");
            }
    }
    public void  show(){
        HttpServletResponse response = ServletActionContext.getResponse();
        PrintWriter writer = null;
        try {
            response.setCharacterEncoding("utf8");
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        StudentService a=new StudentServiceImpl();
        List<Student> all = a.findAll();
        String s = JSON.toJSONString(all);
        writer.print(s);
        System.out.println(s);
    }
    public void insert(){
        StudentService a=new StudentServiceImpl();
        a.insert(u);
    }
    public void page(){
        StudentService st=new StudentServiceImpl();
        PageInfo<Student> page = st.page(pageIndex, pageSize);
        HttpServletResponse response = ServletActionContext.getResponse();
        try {
            response.setCharacterEncoding("utf8");
            PrintWriter writer = response.getWriter();
            String s = JSON.toJSONString(page);
            writer.print(s);
            System.out.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void showStudent(){
        HttpServletResponse response = ServletActionContext.getResponse();
        response.setCharacterEncoding("utf-8");
        try {
            PrintWriter writer = response.getWriter();
            List<Student> show = a.findShow();
            String s = JSON.toJSONString(show);
            writer.print(s);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public Student getU() {
        return u;
    }

    public void setU(Student u) {
        this.u = u;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public StudentService getA() {
        return a;
    }

    public void setA(StudentService a) {
        this.a = a;
    }
}
